package com.ruoyi.project.ypjxc.service.impl;

import com.ruoyi.project.ypjxc.domain.Buy;
import com.ruoyi.project.ypjxc.domain.BuyOrder;
import com.ruoyi.project.ypjxc.domain.Order;
import com.ruoyi.project.ypjxc.mapper.YpjxcBuyMapper;
import com.ruoyi.project.ypjxc.mapper.YpjxcBuyOrderMapper;
import com.ruoyi.project.ypjxc.mapper.YpjxcOrderMapper;
import com.ruoyi.project.ypjxc.service.YpjxcOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class YpjxcOrderServiceImpl implements YpjxcOrderService {
    @Autowired
    private YpjxcOrderMapper orderMapper;

    @Autowired
    private YpjxcBuyMapper buyMapper;

    @Autowired
    private YpjxcBuyOrderMapper buyOrderMapper;

    /**
     * 查询药品订单信息
     * @param order 药品订单信息
     * @return
     */
    @Override
    public List<Order> selectOrderList(Order order) {
        return orderMapper.selectOrderList(order);
    }

    /**
     * 添加药品订单信息
     * @param order 药品订单信息
     * @return
     */
    @Override
    public int insertOrder(Order order) {
        //添加订单表
        String orderId = orderMapper.insertOrder(order);
        //获取订单表中的采购信息表
        List<Buy> buyList = order.getBuyList();
        for (Buy buy : buyList) {
            //添加采购表
            buyMapper.insertBuy(buy);
            //获取采购表id
            Long buyId = buy.getBuyId();
            //添加采购订单关系表
            buyOrderMapper.insertBuyOrder(new BuyOrder(null,buyId,orderId));
        }
        return "0".equals(orderId)?0:1;
    }

    /**
     *
     * 修改药品订单信息
     * @param order 药品订单信息
     * @return
     */
    @Override
    public int updateOrder(Order order) {
        return orderMapper.updateOrder(order);
    }

    /**
     * 根据药品订单id查询
     * @param orderId 药品订单ID
     * @return
     */
    @Override
    public Order selectOrderById(String orderId) {
        return orderMapper.selectOrderById(orderId);
    }

    /**
     * 批量删除药品订单信息
     * @param orderId 需要删除的药品订单ID
     * @return
     */
    @Override
    @Transactional
    public int deleteOrderByIds(Long[] orderId) {
        int i = orderMapper.deleteOrderByIds(orderId);
        orderMapper.deleteOrderDrugByIds(orderId);
        orderMapper.deleteOrderSupplierByIds(orderId);
        return i;
    }


}
